package com.yaha.controler;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yaha.controler.devices.DeviceManager;
import com.yaha.controler.devices.interfaces.DeviceStateListener;
import com.yaha.controler.devices.pojo.MdnsDevice;
import com.yaha.controler.touchpad.TouchpadTouchEventHandlerListener;
import com.yaha.controler.utils.MyLog;
import com.yaha.controler.widgets.WifiPopWindow;

public class MainActivity extends BaseActivity implements DeviceStateListener {
	private static final String TAG = MainActivity.class.getSimpleName();
	/** 设备连接状态 */
	protected static final int MSG_DEVICES_CONNECTED = 1;
	/** 设备未连接状态连接 */
	private static final int MSG_DEVICES_DISCONNECTED = 2;
	private Context mContext;
	private ImageButton mWifiListBtn;
	private Button mDeviceStateBtn;
	private MdnsDevice mCurrentDevice;
	private TouchpadTouchEventHandlerListener mTouchHanddler;
	/** 触摸板Body */
	private LinearLayout mTouchpadBodyImageView;
	/** 触摸点 */
	private ImageView mTouchPoint;
	// 屏幕的宽度和高度
	public int width;
	public int height;
	
	private boolean mIsToLand = false; // 是否横屏
	/** 屏幕状态标识变量 */
	private int ORIENTATION= 0;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_DEVICES_CONNECTED:
				MyLog.v(TAG, "MSG_DEVICES_CONNECTED ");
				// 如果设备为空，再重新获取一下设备
				if (null == mCurrentDevice) {
					mCurrentDevice = DeviceManager.newInstance().getChoiceMdnsDevice();
				}
				updateDevState();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = MainActivity.this;

		mCurrentDevice = DeviceManager.newInstance().getChoiceMdnsDevice();
		DeviceManager.newInstance().addDeviceStateListener(this);

		initView();

		callBackLsitener();
	}

	@Override
	protected void onResume() {
		super.onResume();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
		ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

		MyLog.d(TAG, "Connected Devices===" + DeviceManager.newInstance().getChoiceMdnsDevice());
		if (null != DeviceManager.newInstance().getChoiceMdnsDevice()) {
			Message mess = mHandler.obtainMessage();
			mess.what = MSG_DEVICES_CONNECTED;
			mess.sendToTarget();
		}
	}

	private void initView() {
		mWifiListBtn = (ImageButton) findViewById(R.id.device_list_btn);
		mDeviceStateBtn = (Button) findViewById(R.id.device_state_btn);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels; // 宽
		height = dm.heightPixels; // 高
		// 初始化触摸板相关控件
		mTouchPoint = (ImageView) findViewById(R.id.touch_point);
		mTouchpadBodyImageView = (LinearLayout) findViewById(R.id.touchpad_body_image);

		mTouchHanddler = new TouchpadTouchEventHandlerListener(mTouchPoint, mCurrentDevice, mContext);
		mTouchpadBodyImageView.setOnTouchListener(mTouchHanddler);
	}

	private void callBackLsitener() {
		mWifiListBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MyLog.i(TAG, "mWifiListBtn Click");
				WifiPopWindow addPopWindow = new WifiPopWindow(MainActivity.this);
				addPopWindow.showPopupWindow(mWifiListBtn);
			}
		});

	}

	/**
	 * <功能描述>更新设备连接状态
	 */
	private void updateDevState() {
		if (null == mCurrentDevice) {
			mCurrentDevice = DeviceManager.newInstance().getChoiceMdnsDevice();
		}
		if (mCurrentDevice != null) {
			Log.d(TAG, "updateDevState dev : " + mCurrentDevice);
			mWifiListBtn.setBackgroundResource(R.drawable.devices_icon_connected);
			mDeviceStateBtn.setText(String.format(getString(R.string.device_choice_title), mCurrentDevice.getName()));
		} else {
			mWifiListBtn.setBackgroundResource(R.drawable.devices_icon_disconnected);
			mDeviceStateBtn.setText(R.string.devices_disconnnted);
		}
	}

	// --------------设备监听实现模块 --Begin--------------------------------
	@Override
	public void onDeviceAdd(MdnsDevice device) {

	}

	@Override
	public void onDeviceRemove(MdnsDevice device) {

	}

	@Override
	public void onChioseDevice(MdnsDevice device) {

	}

	// --------------设备监听实现模块--End---------------------------------

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		MyLog.d(TAG, "onConfigurationChanged-->Begin");

	}
}
